<template>
  <div class="login_container">
    <!-- 登录盒子 -->
    <div class="login_box">
      <!-- 图像区域 -->
      <div class="login_icon">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 登录表单区域 -->
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="100px" class="login_form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" type="text"></el-input>
        </el-form-item>
        <!-- 为item项指定一个prop属性，等于一个具体的验证规则 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" round @click="login">登录</el-button>
          <el-button type="success" round @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      // 存储登录信息
      loginForm: {
        username: "admin",
        password: "123456"
      },
      // 校验规则
      rules: {
        // 用户名
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min:3, max: 10, message: '长度在3-10个字符', trigger: 'blur'}
        ],
        // 密码
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min:3, max: 10, message: '长度在3-10个字符', trigger: 'blur'}
        ]
      }
    }
  },

  methods: {
    // 点击按钮重置
    resetLoginForm() {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },

    // 处理登录按钮
     login() {
      // 登录之前表单预验证
      this.$refs.loginFormRef.validate(async valid=> {
        // console.log(valid)
        if(!valid) return;
        // 发起数据请求
        const {data: res} =  await this.$http.post('login',this.loginForm)
        // console.log(res)
        if(res.meta.status!==200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // 将登录成功之后的token保存到sessionStorage中
        // 通过编程时导航跳转到后台页面
        window.sessionStorage.setItem('token',res.data.token)
        this.$router.push('/home')
      })
    }
  }

  
};
</script>


// 加上 scoped，代表该样式只在该文件内生效，防止样式冲突
<style lang="less" scoped>
.login_container {
  // background: blueviolet;
  // 背景渐变
  background: -webkit-linear-gradient(left,blueviolet, purple);
  height: 100%;
  position: relative;
}

.login_box {
  width: 540px;
  height: 400px;
  background: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 4%;

  .login_icon {
    position: absolute;
    // top: -50px;
    left: 50%;
    width: 150px;
    height: 150px;
    background: white;
    border: 1px solid #eee;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0 0 10px #ddd;
    padding: 10px;

    img {
      width: 100%;
      height: 100%;
      background: #eee;
      border-radius: 50%;
    }
  }
}


.login_form  {
  position: absolute;
  bottom: 0;
  width: 100%;
  // 上右下左
  padding: 0 30px 30px 0;
  box-sizing: border-box;
}


.btns {
  display: flex;
  justify-content: flex-end;
}

</style>